/************************************************************************************/
/*  Program name CAPFNDEXT_PROFILE.sql                                              */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDEXT_PROFILE';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPFND_PROFILE.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT
		R.Responsibility_Key,
		'PER_BUSINESS_GROUP_ID'		Opt_Type,
		B.Name						Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		PER_BUSINESS_GROUPS			B
	WHERE	O.Profile_Option_Name				=	'PER_BUSINESS_GROUP_ID'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		V.Profile_Option_Value				=	B.Business_Group_ID
	UNION
	SELECT
		R.Responsibility_Key,
		'PER_SECURITY_PROFILE_ID'	Opt_Type,
		B.Security_Profile_Name		Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		PER_SECURITY_PROFILES		B
	WHERE	O.Profile_Option_Name				=	'PER_SECURITY_PROFILE_ID'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		V.Profile_Option_Value				=	B.Security_Profile_ID
	UNION
	SELECT
		R.Responsibility_Key,
		'ORG_ID'					Opt_Type,
		B.Name						Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		HR_ALL_ORGANIZATION_UNITS	B
	WHERE	O.Profile_Option_Name				=	'ORG_ID'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		V.Profile_Option_Value				=	B.Organization_ID
	UNION
	SELECT
		R.Responsibility_Key,
		'GL_SET_OF_BKS_NAME'		Opt_Type,
		B.Short_Name				Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		GL_SETS_OF_BOOKS			B
	WHERE	O.Profile_Option_Name				=	'GL_SET_OF_BKS_NAME'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		V.Profile_Option_Value				=	B.Name
	UNION
	SELECT
		R.Responsibility_Key,
		'GL_SET_OF_BKS_ID'		Opt_Type,
		B.Short_Name				Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		GL_SETS_OF_BOOKS			B
	WHERE	O.Profile_Option_Name				=	'GL_SET_OF_BKS_ID'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		V.Profile_Option_Value				=	B.SET_OF_BOOKS_ID
	UNION
	SELECT
		R.Responsibility_Key,
		'BUDGETARY_CONTROL_OPTION'	Opt_Type,
		B.BC_Option_Name			Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		GL_BC_OPTIONS				B
	WHERE	O.Profile_Option_Name				=	'BUDGETARY_CONTROL_OPTION'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		V.Profile_Option_Value				=	B.BC_Option_ID
	UNION
	SELECT
		R.Responsibility_Key,
		'PO_AUTOCREATE_DATE'		Opt_Type,
		V.Profile_Option_Value		Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V
	WHERE	O.Profile_Option_Name				=	'PO_AUTOCREATE_DATE'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	UNION
	SELECT
		R.Responsibility_Key,
		'CURRENCY:NEGATIVE_FORMAT'	Opt_Type,
		B.Meaning					Name,
		V.*
	FROM
		FND_RESPONSIBILITY_VL		R,
		FND_PROFILE_OPTIONS			O,
		FND_PROFILE_OPTION_VALUES	V,
		FND_LOOKUPS					B
	WHERE	O.Profile_Option_Name				=	'CURRENCY:NEGATIVE_FORMAT'
	AND		O.Application_ID					=	V.Application_ID
	AND		O.Profile_Option_ID					=	V.Profile_Option_ID
	AND		10003								=	V.Level_ID
	AND		R.Responsibility_ID					=	V.Level_Value
	AND		SUBSTR(R.Responsibility_Key,1,4)	=	p_NGO
	AND		'CURRENCY:NEGATIVE_FORMAT'			=	B.Lookup_Type
	AND		V.Profile_Option_Value				=	B.Lookup_Code
	ORDER BY 1, 2;

BEGIN
	lv_NGO			:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Responsibility_Key||CHR(9)||
				C1.Opt_Type||CHR(9)||
				C1.Name||CHR(9)||
				C1.APPLICATION_ID||CHR(9)||
				C1.PROFILE_OPTION_ID||CHR(9)||
				C1.LEVEL_ID||CHR(9)||
				C1.LEVEL_VALUE||CHR(9)||		--Resp ID
				C1.LAST_UPDATE_DATE||CHR(9)||
					C1.LAST_UPDATED_BY||CHR(9)||
				C1.CREATION_DATE||CHR(9)||
					C1.CREATED_BY||CHR(9)||
				C1.LAST_UPDATE_LOGIN||CHR(9)||
				C1.PROFILE_OPTION_VALUE||CHR(9)||
				C1.LEVEL_VALUE_APPLICATION_ID||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Profile Options for for this NGO';
		END IF;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/